package lumis.search.filter;

import java.util.Collections;
import java.util.List;

import lumis.portal.stability.StableMinor;
import lumis.search.Searcher;

/**
 * Allows the creation of composite filters by grouping them together 
 * and using a logical operator to join them.
 * 
 * @version $Revision: 13082 $ $Date: 2011-05-27 16:03:12 -0300 (Fri, 27 May 2011) $
 * @since 4.2.2
 */
@StableMinor(version = "6.0", sinceVersion = "4.2.2")
public class SearchFilterGroup implements ISearchQueryFilter
{
	/**
	 * Performs a logical AND when grouping the filters.
	 * @since 4.2.2
	 */
	public static final String GROUP_OPERATOR_AND = "and";
	
	/**
	 * Performs a logical OR when grouping the filters.
	 * @since 4.2.2
	 */
	public static final String GROUP_OPERATOR_OR = "or";
	
	private String operator = GROUP_OPERATOR_AND;
	private List<? extends ISearchQueryFilter> filters = Collections.emptyList();
	
	/**
	 * Returns the operator for grouping the filters.
	 * @return the operator.
	 * @see #setOperator(String)
	 * @since 4.2.2
	 */
	public String getOperator()
	{
		return operator;
	}
	
	/**
	 * Sets the operator for grouping the filters. The default value is {@link #GROUP_OPERATOR_AND}.
	 * <p>
	 * This class contains some operator constants defined as standard operators
	 * for clauses. The actual allowed operators and interpretation given 
	 * depend on the {@link Searcher} implementation being used. 
	 * It is recommended that all searchers implement the standard 
	 * operators defined in this class for best compatibility.
	 * 
	 * @param operator the operator value to set.
	 * @see #GROUP_OPERATOR_AND
	 * @see #GROUP_OPERATOR_OR
	 * 
	 * @since 4.2.2
	 */
	public void setOperator(String operator)
	{
		this.operator = operator;
	}

	/**
	 * Returns the filters in this group.
	 * @return a immutable list containing the filters.
	 * @since 4.2.2
	 */
	public List<? extends ISearchQueryFilter> getFilters()
	{
		return filters;
	}

	/**
	 * Sets the filters in this group.
	 * @param filters a list containing the filters.
	 * @since 4.2.2
	 */
	public void setFilters(List<? extends ISearchQueryFilter> filters)
	{
		if(filters == null)
			throw new IllegalArgumentException("Invalid filters: null list");
		
		if(filters.contains(null))
			throw new IllegalArgumentException("Invalid filter: null value");
		
		this.filters = Collections.unmodifiableList(filters);
	}
}
